package org.tity.test.entity;

import java.io.File;
import java.util.Date;

import org.tity.commons.environment.PathTool;
import org.tity.commons.environment.ProjectInfo;
import org.tity.sql.entity.JoinImplType;
import org.tity.sql.entity.builder.EntityCodeFactory;
import org.tity.sql.entity.builder.JoinRelation;
import org.tity.sql.entity.builder.RelationType;
import org.tity.test.config.Configs;
import org.tity.test.config.AppDAO;

public class EntityBuilder {
	
	private static EntityCodeFactory fac;
	
	public static void main(String[] args) throws Exception {
		
		fac = init();
		setBasicQuery();
		define();
		build();
	}

	private static void setBasicQuery() {
		 
		fac.setBasicQuery("hr_person", "select * from hr_person where deleted='N'");
//		fac.addValidate("hr_person","name",new XX());
		//
		fac.setBasicQuery("sys_user", "select * from sys_user where deleted='N'");
		
		
	}

	private static void define() throws Exception {
		//设置关联关系
		JoinRelation r=null;
		
		r = new JoinRelation("sys_user","hr_person",null); //构建并设定关联表
		r.addJoinFields("id", "id"); //设定关联字段
		r.setJoinImplType(JoinImplType.AUTO);
		r.setRelation(RelationType.ONE_TO_ONE,RelationType.ONE_TO_ONE);//设定两表正向与反向关联关系
		fac.addRelation(r); //加入
		
		
		
		r = new JoinRelation("sys_dict","sys_dict_item",null); //构建并设定关联表
		r.addJoinFields("id", "dict_id"); //设定关联字段
		r.setGetMethodName("getDictItems", "getOwnerDict");
		r.setJoinMethodName("joinDictItems", "joinOwnerDict");
		r.setJoinImplType(JoinImplType.AUTO);
		r.setRelation(RelationType.ONE_TO_MANY,RelationType.ONE_TO_ONE);//设定两表正向与反向关联关系
		fac.addRelation(r); //加入
		
		
		r = new JoinRelation("sys_user","sys_dict_item",null); //构建并设定关联表
		r.setQueryB("select * from sys_dict_item where dict_id=?", "ADV_RESERVE_AREA"); //设定查询，用于过滤条件和字段，不设置时直接查表
		r.addJoinFields("area_type", "id"); //设定关联字段
		r.setJoinImplType(JoinImplType.AUTO); //设置连接实现方式，某些情况下存在强制使用 ByValue
		r.setRelation(RelationType.ONE_TO_ONE,RelationType.ONE_TO_MANY);//设定两表正向与反向关联关系
		fac.addRelation(r); //加入
		
		r = new JoinRelation("sys_user","sys_dict_item",null); //构建并设定关联表
		r.setQueryB("select * from sys_dict_item where dict_id=?", "CARD_TYPE"); //设定查询，用于过滤条件和字段，不设置时直接查表
		r.addJoinFields("card_type", "id"); //设定关联字段
		r.setJoinImplType(JoinImplType.AUTO); //设置连接实现方式，某些情况下存在强制使用 ByValue
		r.setRelation(RelationType.ONE_TO_ONE,RelationType.ONE_TO_MANY);//设定两表正向与反向关联关系
		fac.addRelation(r); //加入
 
		//通过关系表进行关联
		r = new JoinRelation("sys_role","sys_user","sys_roles_to_user"); //构建并设定关联表
		r.addJoinFields( "id",null,"role_id"); //设定关联字段
		r.addJoinFields( null,"id","user_id"); //设定关联字段
		r.setRelationQuery("select * from sys_roles_to_user where mgr_level in(?,?,?)", "A","B","C");
		r.setJoinImplType(JoinImplType.AUTO);
		r.setRelation(RelationType.ONE_TO_MANY,RelationType.ONE_TO_MANY);//设定两表正向与反向关联关系
		fac.addRelation(r); //加入

//		r = new JoinRelation("sys_dict_item","sys_user",null); //构建并设定关联表
//		r.addJoinFields("id","card_type"); //设定关联字段
//		r.setJoinImplType(JoinImplType.Auto);
//		r.setRelation(RelationType.ONE_TO_MANY,RelationType.ONE_TO_ONE);//设定两表正向与反向关联关系
//		fac.addRelation(r); //加入
 
		 
	}
	
	
	
	

	private static EntityCodeFactory init() {
		//设置数据库连接
		Configs config=new Configs();
		AppDAO dao=new AppDAO();
		dao.setDataSource(config.getDataSource());
		
		//创建代码工厂
		EntityCodeFactory fac=new EntityCodeFactory(dao);
		String path="";
		
		//设置RawEntity的生成目录与包
		File rawEntityDir=ProjectInfo.getMavenSourceDir(EntityBuilder.class).resolve("raw").file();
//		path=rawEntityDir.getAbsolutePath();
//		path=path.replace("target\\classes", "src\\main\\java");
//		path=path.replace("target/classes", "src/main/java");
//		rawEntityDir=new File(path+"/raw");
		fac.setEntityDir(rawEntityDir); //设置生成目录
		fac.setEntityPkg("org.tity.test.entity.raw"); //设置包名
		System.out.println("raw:"+rawEntityDir.getAbsolutePath());
		
//		fac.setConfigPath(ProjectInfo.getMavenSourceDir(EntityBuilder.class).resolve("config.json").file());
		
//		//设置BusiEntity的生成目录与包
//		File busiEntityDir=PathTool.getDir(EntityBuilder.class);
//		path=busiEntityDir.getAbsolutePath();
//		path=path.replace("target\\classes", "src\\main\\java");
//		path=path.replace("target/classes", "src/main/java");
//		busiEntityDir=new File(path+"/busi");
//		fac.setBusiEntityDir(busiEntityDir);//设置生成目录
//		fac.setBusiEntityPkg("org.tity.test.entity.busi"); //设置包名
//		System.out.println("busi:"+busiEntityDir.getAbsolutePath());
		return fac;
	}
	
	private static void build() throws Exception {
		fac.buildAll();
	}

}
